/*
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package ru.irenproject;

import javax.inject.Inject;

import ru.irenproject.it.ItInput;
import ru.irenproject.it.ItInputException;
import ru.irenproject.it.ItOutput;
import ru.irenproject.store.AbstractThing;
import ru.irenproject.store.AspectId;
import ru.irenproject.store.Event;
import ru.irenproject.store.Initializer;
import ru.irenproject.store.Stash;
import ru.irenproject.store.Thing;

import com.google.protobuf.Message;

public final class SectionEditor extends AbstractThing implements Visual {
  public static final class SetNameInput {
    public String name;
  }

  public static final class GetModifiersOutput {
    public byte[] modifiers;
  }

  public static final class SetModifiersInput {
    public byte[] modifiers;
  }

  private static final AspectId ASPECT_ID = new AspectId(0xfa35b8c0a751d35bL);

  public static SectionEditor open(final Section section) {
    return Utils.requireAspect(section, ASPECT_ID, SectionEditor.class, new Initializer<SectionEditor>() {
      @Override public void initialize(SectionEditor target) {
        target.prepare(section);
      }
    });
  }

  @Stash private UiProtos.SectionEditor.Builder $;
  private final ItOutput fItOutput;
  private final ItInput fItInput;

  @Inject SectionEditor(ItOutput itOutput, ItInput itInput) {
    fItOutput = itOutput;
    fItInput = itInput;
  }

  private void prepare(Section section) {
    $.setSection(section.id());
    section.addListener(this);
    section.questionList().addListener(this);
    section.modifierList().addListener(this);
  }

  public Section section() {
    return get($.getSection(), Section.class);
  }

  @Override public Message render() {
    Section section = section();
    return UiProtos.SectionEditorShape.newBuilder()
        .setName(section.name())
        .setQuestionCount(section.questionList().itemCount())
        .setHasModifiers(!section.modifierList().isEmpty())
        .build();
  }

  @Action(badInput = TestInputException.class)
  public void doSetName(SetNameInput in) {
    section().setName(Check.inputNotNull(in.name));
  }

  @Override protected void handleEvent(Event e, Thing source) {
    if ((e instanceof Section.ChangeEvent) || (e instanceof QuestionList.ChangeEvent)
        || (e instanceof ModifierList.ChangeEvent)) {
      markForRender();
    }
  }

  @Action(markAsModified = false)
  public GetModifiersOutput doGetModifiers() {
    GetModifiersOutput res = new GetModifiersOutput();
    res.modifiers = fItOutput.modifierListToBytes(section().modifierList());
    return res;
  }

  @Action(badInput = ItInputException.class)
  public void doSetModifiers(SetModifiersInput in) {
    fItInput.bytesToModifierList(Check.inputNotNull(in.modifiers), section().modifierList());
  }
}
